package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author 宋
 * @since 2022-06-06
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;


    /**
     * 添加套餐
     * Request URL: http://127.0.0.1:8080/setmeal
     * Request Method: POST
     * <p>
     * {name: "1111", categoryId: "1534937248303394817", price: 111100, code: "",…}
     * categoryId: "1534937248303394817"
     * code: ""
     * description: "111"
     * dishList: []
     * //idType: "1534937248303394817"
     * image: "2022-06-16/89d7eb8e-af29-4070-8bc2-bb335bd71eae.jpg"
     * name: "1111"
     * price: 111100
     * //setmealDishes: [{copies: 1, dishId: "1397850392090947585", name: "组庵鱼翅1", price: 4800},…]
     * 0: {copies: 1, dishId: "1397850392090947585", name: "组庵鱼翅1", price: 4800}
     * 1: {copies: 1, dishId: "1397860963880316929", name: "脆皮乳鸽", price: 10800}
     * status: 1
     */

    @PostMapping
    public R<String> add() {
        // TODO  没写了
        return null;
    }


    /**
     * 对应套餐分类 -->> 回显套餐
     * Request URL: http://127.0.0.1:8888/setmeal/list?categoryId=1534937248303394817&status=1
     * Request Method: GET
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(@RequestParam Map map) {
        //查询对应分类的套餐 回显给前端展示
        System.out.println(111);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, map.get("categoryId")).eq(Setmeal::getStatus, map.get("status"));
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * Request URL: http://127.0.0.1:8080/setmeal/page?page=1&pageSize=10
     * Request Method: GET
     * page: 1
     * pageSize: 10
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        // 1. 1分页构造器
        Page<Setmeal> page1 = new Page<>(page, pageSize);

        // 1 . 2  条件构造器    select * from  Dish
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        //判断是否传入了name 进行模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);

        setmealService.page(page1, queryWrapper);

        // 2 . 对应的菜品分类id
        HashSet<Long> hashSet = new HashSet<>();   //存 CategoryId集合
        HashMap<Long, String> hashMap = new HashMap<>();  //存 对应关系 getCategoryId = name
        List<Setmeal> records = page1.getRecords();
        //getCategoryId集合  -->> 查出getCategoryId对应的name -->>  建立对应关系 getCategoryId = name -->>
        // 再用每个对象的getCategoryId取出name 赋值给冗余字段
        for (Setmeal setmeal : records) {
            hashSet.add(setmeal.getCategoryId());
        }
        List<Category> categories = categoryService.listByIds(hashSet);
        for (Category category : categories) {
            hashMap.put(category.getId(), category.getName());
        }
        for (Setmeal setmeal : records) {
            String categoryNmae = hashMap.get(setmeal.getCategoryId());
            setmeal.setCategoryName(categoryNmae);
        }
        page1.setRecords(records);

        return R.success(page1);
    }
}

